using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

interface XEditorAvatarTab
{
    void OnEnable(Rect rect);
    void OnDisable();
    void OnGUI(Rect rect);
    void OnReload();
}

public class XEditorBuildAvatarInspectorWindow : EditorWindow
{
    [MenuItem("Window/Avatar")]
    static void OpenEditor()
    {
        XEditorBuildAvatarInspectorWindow build = GetWindow<XEditorBuildAvatarInspectorWindow>();
        build.titleContent = new GUIContent("Avatar");
        build.Show();
    }

    /// <summary>
    /// 
    /// </summary>
    enum BuildMode
    {
        Avatar,
        Attachment,
        All
    }
    BuildMode buildMode = BuildMode.Avatar;

    /// <summary>
    /// 
    /// </summary>
    List<string> buildModeLabs = new List<string>();
    Dictionary<BuildMode, XEditorAvatarTab> buildTabs = new Dictionary<BuildMode, XEditorAvatarTab>();    
    Texture2D refreshTexture;

    /// <summary>
    /// 
    /// </summary>
    public XEditorBuildAvatarInspectorWindow()
    {
        for(BuildMode mode=BuildMode.Avatar; mode<BuildMode.All; mode++)
        {
            buildModeLabs.Add(mode.ToString());
        }

        buildTabs.Add(BuildMode.Avatar, new XEditorBuildAvatarTab(this));
        buildTabs.Add(BuildMode.Attachment, new XEditorBuildAttachmentTab(this));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    private Rect GetSubWindowArea()
    {
        Rect subPos = new Rect(0, 0, position.width, position.height);
        return subPos;
    }

    /// <summary>
    /// 
    /// </summary>
    private void OnEnable()
    {
        refreshTexture = EditorGUIUtility.FindTexture("Refresh");

        Rect subPos = GetSubWindowArea();
        foreach(KeyValuePair<BuildMode, XEditorAvatarTab> v in buildTabs)
        {
            v.Value.OnEnable(subPos);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    private void OnDisable()
    {
        foreach (KeyValuePair<BuildMode, XEditorAvatarTab> v in buildTabs)
        {
            v.Value.OnDisable();
        }
    }

    /// <summary>
    /// 
    /// </summary>
    private void OnGUI()
    {
        Rect subPos = GetSubWindowArea();

        buildMode = (BuildMode)GUILayout.Toolbar((int)buildMode, buildModeLabs.ToArray());
        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        if (GUILayout.Button(refreshTexture, GUILayout.MaxWidth(30)))
        {
            foreach(KeyValuePair<BuildMode, XEditorAvatarTab> tab in buildTabs)
            {
                tab.Value.OnReload();
            }
        }
        GUILayout.EndHorizontal();

        switch (buildMode)
        {
            case BuildMode.Avatar:
                if (buildTabs.ContainsKey(BuildMode.Avatar))
                    buildTabs[BuildMode.Avatar].OnGUI(subPos);
                break;

            case BuildMode.Attachment:
                if (buildTabs.ContainsKey(BuildMode.Attachment))
                    buildTabs[BuildMode.Attachment].OnGUI(subPos);
                break;
        }
    }

    int fps = 0;
    private void Update()
    {
        fps++;
        if (fps >= 10)
        {
            Repaint();
            fps = 0;
        }
    }
}
